package watermark;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import watermark.CommonButton.ButtonStyle;

/**
 * 选择插入文字水印后的操作窗体
 * @author syt
 *
 */
public class MainFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private String toward[] = { "右下", "左下", "右上", "左上", "正中", "左上->右下",
            "左下->右下" };
    private String fontstyle[] = {"普通","粗体","斜体","粗体&斜体"};
    private int fontstyles[] = {Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD | Font.ITALIC};
    private String fontcolor[] = {"蓝色","青色","绿色","红色","灰色","深灰色","浅灰色",
            "洋红色","桔黄色","粉红色","黄色","黑色","白色"};
    private String scales[] = {"不缩放", "25%", "50%", "75%","125%","150%","175%","200%"};
    private float imgScales[] = {1.0f, 0.25f, 0.5f, 0.75f, 1.25f, 1.5f, 1.75f, 2.0f};
    private Color colors[] = {Color.blue, Color.cyan, Color.green, Color.red, Color.gray,
            Color.darkGray, Color.lightGray, Color.magenta, Color.orange, Color.pink,
            Color.yellow, Color.black, Color.white};
    private JComboBox fontCB, towardCB, fontstyleCB, fontcolorCB, scaleCB;
    private JTextField filepathTF, savepathTF, markTF, fontsizeTF;
    private JSlider alphaSlider;
    private Font font = new Font("微软雅黑", Font.PLAIN, 14);
    private Border border = new BevelBorder(BevelBorder.RAISED);// 简单的双线斜面边框
    private int mainFrameWidth = 550;// MainFrame的宽度
    private int mainFrameHeight = 410;// MainFrame的高度
    private JButton selectPathBT, batchingBT;
    private float alpha = 0.5f;
    private JProgressBar progressBar;

    //关于组件外观的枚举
    protected enum UIClassName {
        systemLookAndFeel(UIManager.getSystemLookAndFeelClassName()),
        motifLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"),
        crossPlatformLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()),
        metalLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        private String className;

        UIClassName(String className) {
            this.className = className;
        }

        public String getUIClassName() {
            return className;
        }
    }

    private static final MainFrame mainFrame = new MainFrame();
    public static MainFrame getInstance() {
        return mainFrame;
    }

    private MainFrame() {
        // 设置外观
        setUI(UIClassName.systemLookAndFeel);
        setTitle("添加水印");
        setSize(mainFrameWidth, mainFrameHeight);
        Common.setCentered(this);
        setLayout(new BorderLayout());

        JPanel panel = getNorthPanel();
        add(panel, BorderLayout.NORTH);

        panel = getCenterPanel();
        add(panel, BorderLayout.CENTER);

        panel = getSouthPanel();
        add(panel, BorderLayout.SOUTH);

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    private JPanel getNorthPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.setBorder(new TitledBorder(border, "文件存取",
                Font.LAYOUT_LEFT_TO_RIGHT, Font.LAYOUT_LEFT_TO_RIGHT, font));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 10, 5, 10);
        gbc.gridx = 0;
        gbc.weightx = 1;

        String filepath = WaterMark.getInstance().getSrcImgPath();
        filepathTF = new JTextField(filepath);
        panel.add(filepathTF, gbc);

        String savePath = filepath.substring(0,filepath.lastIndexOf("\\")) + Common.getNewFileorDirName(filepath);
        savepathTF = new JTextField(savePath);
        panel.add(savepathTF, gbc);

        gbc.gridx = 1;
        gbc.weightx = 0;

        selectPathBT = makeButton(ButtonStyle.selectImage, panel, gbc);
        makeButton(ButtonStyle.selectSavepath, panel, gbc);
        Common.setComponentsFont(panel, font);
        return panel;

    }

    private JPanel getCenterPanel() {

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.setBorder(new TitledBorder(border, "设置",
                Font.LAYOUT_LEFT_TO_RIGHT, Font.LAYOUT_LEFT_TO_RIGHT, font));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 10, 5, 10);
        gbc.fill = GridBagConstraints.BOTH;

        gbc.gridwidth = 1;
        JLabel label;
        label = new JLabel("选择字体:", JLabel.RIGHT);
        panel.add(label, gbc);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        fontCB = new JComboBox(Common.getAvailableFontFamilyNames());
        panel.add(fontCB, gbc);

        gbc.weightx = 1.0;
        gbc.gridwidth = 1;

        label = new JLabel("字体大小:", JLabel.RIGHT);
        panel.add(label, gbc);

        fontsizeTF = new JTextField("15");
        panel.add(fontsizeTF, gbc);

        label = new JLabel("水印位置:", JLabel.RIGHT);
        panel.add(label, gbc);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        towardCB = new JComboBox(toward);
        panel.add(towardCB, gbc);

        gbc.gridwidth = 1;
        label = new JLabel("字体样式:", JLabel.RIGHT);
        panel.add(label, gbc);

        fontstyleCB = new JComboBox(fontstyle);
        panel.add(fontstyleCB, gbc);

        label = new JLabel("水印颜色:", JLabel.RIGHT);
        panel.add(label, gbc);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        fontcolorCB = new JComboBox(fontcolor);
        panel.add(fontcolorCB, gbc);

        gbc.weightx = 1.0;
        gbc.gridwidth = 1;
        label = new JLabel("透明度:", JLabel.RIGHT);
        panel.add(label, gbc);
        alphaSlider = new JSlider(JSlider.HORIZONTAL);
        alphaSlider.addChangeListener(new ChangeListener(){
            public void stateChanged(ChangeEvent e) {
                alpha = (float)alphaSlider.getValue()/100;
            }
        });
        panel.add(alphaSlider, gbc);

        label = new JLabel("缩放大小:", JLabel.RIGHT);
        panel.add(label, gbc);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        scaleCB = new JComboBox(scales);
        panel.add(scaleCB, gbc);

        gbc.gridwidth = 1;
        label = new JLabel("水印内容：", JLabel.RIGHT);
        panel.add(label, gbc);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        markTF = new JTextField("Hello World");
        panel.add(markTF, gbc);

        gbc.gridwidth = 1;
        label = new JLabel("执行进度:", JLabel.RIGHT);
        panel.add(label, gbc);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        progressBar = new JProgressBar(0, 1);
        progressBar.setStringPainted(true);
        panel.add(progressBar, gbc);
        Common.setComponentsFont(panel, font);
        return panel;
    }

    private JPanel getSouthPanel() {
        JPanel panel = new JPanel();
        makeButton(ButtonStyle.preview, panel);
        makeButton(ButtonStyle.drirect, panel);
        Common.setComponentsFont(panel, font);
        return panel;
    }
    /**
     * 制按钮 两个参数
     * @param style 枚举ButtonStyle中的一个值
     * @param panel 放置该按钮的JPanel
     * @return JButton
     */
    private JButton makeButton(ButtonStyle style, JPanel panel) {
        return makeButton(style, panel, null);
    }
    /**
     * 制按钮 三个参数
     * @param style 枚举ButtonStyle中的一个值
     * @param panel 放置该按钮的JPanel
     * @param constraints 布局
     * @return JButton
     */
    private JButton makeButton(ButtonStyle style, JPanel panel, Object constraints) {
        JButton button = new CommonButton(style);
        if (constraints == null) {
            panel.add(button);
        } else {
            panel.add(button, constraints);
        }
        return button;
    }

    /**
     * 设置外观
     * @param className 枚举UIClassName
     */
    protected void setUI(UIClassName className) {
        try {
            UIManager.setLookAndFeel(className.getUIClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取所要添加水印的文件名
     * @return
     */
    public String getFilepath() {
        return filepathTF.getText();
    }
    /**
     * 获取添加水印后保存文件的路径
     * @return
     */
    public String getSavePath() {
        return savepathTF.getText();
    }
    /**
     * @return 所要添加的水印的字体
     */
    public Font getWaterMarkFont() {
        return new Font(getFontname(), getFontstyle(), getFontsize());
    }
    /**
     * @return 所要添加的水印的内容
     */
    public String getMark() {
        return markTF.getText();
    }
    /**
     * @return 放置将要添加水印的文件路径的JTextField
     */
    public JTextField getFilepathTF() {
        return filepathTF;
    }
    /**
     * @return 放置将要添加水印后保存路径的JTextField
     */
    public JTextField getSavepathTF() {
        return savepathTF;
    }
    /**
     * @return 水印的颜色
     */
    public Color getFontcolor() {
        return colors[fontcolorCB.getSelectedIndex()];
    }
    /**
     * @return 水印的位置
     */
    public int getToward() {
        return towardCB.getSelectedIndex();
    }
    /**
     * @return 水印所选择的字体名字
     */
    private String getFontname() {
        return String.valueOf(fontCB.getSelectedItem());
    }
    /**
     * @return 水印所使用的字体样式
     */
    private int getFontstyle() {
        return fontstyles[fontstyleCB.getSelectedIndex()];
    }
    /**
     * @return 水印所使用的字体大小
     */
    private int getFontsize() {
        return (int) (Integer.parseInt(fontsizeTF.getText())*getScale());
    }
    /**
     *
     * @return 水印透明度
     */
    public float getWaterMarkAlpha() {
        return alpha ;
    }
    /**
     *
     * @return 放缩比例
     */
    public float getScale() {
        return imgScales[scaleCB.getSelectedIndex()];
    }
    /**
     *
     * @return 进度条
     */
    public JProgressBar getProgressBar() {
        return progressBar;
    }
}

